package com.example.yichuangyun.application;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.multidex.MultiDex;

import com.amap.api.location.AMapLocationClient;
import com.example.yichuangyun.config.Constant;
import com.example.yichuangyun.config.UserManager;
import com.example.yichuangyun.mvvm.di.AppComponent;
import com.example.yichuangyun.mvvm.di.DaggerAppComponent;
import com.example.yichuangyun.mvvm.di.retrofit.RetrofitModule;
import com.example.yichuangyun.utils.ActivityStackManager;
import com.igexin.sdk.PushManager;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.umeng.analytics.MobclickAgent;
import com.umeng.commonsdk.UMConfigure;
import com.example.yichuangyun.mvvm.di.AppModule;

public class MyApplication extends Application implements Application.ActivityLifecycleCallbacks {
    private static MyApplication instance;
    private AppComponent mAppComponent;
    private static Context context;
    public static UserManager manager;
    private int activityCount;//activity的count数
    public static boolean isForeground;//是否在前台

    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
        instance = this;
        initComponent();
        manager = UserManager.getInstance();
        MultiDex.install(this);
        // 我们建议开发者在 Application.onCreate() 和主 Activity.onCreate() 方法中初始化个推 SDK。多次调用 SDK 初始化并无影响。
        PushManager.getInstance().initialize(context);
        registerActivityLifecycleCallbacks(this);
//        // 设置LOG开关，默认为false。
//        UMConfigure.setLogEnabled(true);
        // 友盟预初始化
        UMConfigure.preInit(this, Constant.UMENG_APP_KEY, "Umeng");
        UMConfigure.init(this, Constant.UMENG_APP_KEY, "Umeng", UMConfigure.DEVICE_TYPE_PHONE, "");
        MobclickAgent.setPageCollectionMode(MobclickAgent.PageMode.AUTO);
        AMapLocationClient.updatePrivacyShow(context, true, true);
        AMapLocationClient.updatePrivacyAgree(context, true);
    }

    public static Context getContext() {
        return context;
    }

    public static MyApplication getInstance() {
        return instance;
    }

    private void initComponent() {
        this.mAppComponent = DaggerAppComponent
                .builder()
                .retrofitModule(new RetrofitModule())
                .appModule(new AppModule(this))
                .build();
    }

    public AppComponent getAppComponent() {
        return this.mAppComponent;
    }

    static {
        //设置全局的Header和Footer构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator((context, layout) -> new ClassicsHeader(context));
        SmartRefreshLayout.setDefaultRefreshFooterCreator((context, layout) -> new ClassicsFooter(context));
    }

    //添加Activity到容器中
    public void addActivity(Activity activity) {
        ActivityStackManager.getInstance().pushActivity(activity);
    }

    //遍历所有Activity并finish
    public void exit() {
        ActivityStackManager.getInstance().exitActivity();
    }

    //弹出除一个以外的所有活动
    public void popAllActivityExceptOne() {
        ActivityStackManager.getInstance().popAllActivityExceptMain();
    }

    //退出app
    public void exitApp() {
        ActivityStackManager.getInstance().exitActivity();
        System.exit(0);
    }

    @Override
    public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {

    }

    @Override
    public void onActivityStarted(@NonNull Activity activity) {
        activityCount++;
    }

    @Override
    public void onActivityResumed(@NonNull Activity activity) {
        isForeground();
    }

    @Override
    public void onActivityPaused(@NonNull Activity activity) {

    }

    @Override
    public void onActivityStopped(@NonNull Activity activity) {
        activityCount--;
        isForeground();
    }

    @Override
    public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {

    }

    @Override
    public void onActivityDestroyed(@NonNull Activity activity) {

    }

    /**
     * 判断是否在前台
     */
    private void isForeground() {
        isForeground = activityCount > 0;
    }
}
